﻿	Function Get_MOReg_Info
{
param($property)
$logfrom = "Get_MOReg_Info"
If (($property -ne "Destination") -and ($property -ne "Mask") -and ($property -ne "BackedUpDefaultRoute")){
	[string]$value = (Get-ItemProperty -Path registry::$MORegRoot -ErrorAction SilentlyContinue).$property
	If (!$value){#valeurs par défaut si clés de registres non présentes/vides
		#Write-Logfile($logfrom+" -- Warning: Property "+$property+" does not exist in registry...using default value")
		Switch($property){
		"VPNs"{$value="vpnluxc1.europarl.europa.eu:12121;vpnbruc1.europarl.europa.eu:12121"} 
		"URL"{$value="http://www.europarl.europa.eu;european parliament"}
		"LogFile"{$value="C:\DGPE\Log\Mobile Office\MO7Log.log"}
		"Timeoutconnect"{2000} #2s
		"maxLogSize"{524288} #512kb
		"DefaultIE_StartPage"{$value="http://www.europarl.ep.ec"}
		"OpenIE_StartPage"{$value="http://www.google.com"}
		"Services2Watch"{$value="NlaSvc;netprofm"}
		"ServicesWatcherEnabled"{$value=1}
		}
	}
}
ElseIf($property -eq "DefaultConnectionSettings"){
	[Byte[]]$value = (Get-ItemProperty -Path registry::$MORegRoot -ErrorAction SilentlyContinue).$property
	If (!$value){
		#Write-Logfile($logfrom+" -- Warning: Property "+$property+" does not exist in registry...using default value")
		[Byte[]]$value = '70 0 0 0 103 93 0 0 7 0 0 0 0 0 0 0 49 0 0 0 42 46 101 112 46 112 97 114 108 46 117 110 105 111 110 46 101 117 59 42 46 101 112 46 101 99 59 49 51 54 46 49 55 51 46 42 59 49 48 46 42 59 60 108 111 99 97 108 62 33 0 0 0 104 116 116 112 58 47 47 112 114 111 120 121 100 121 110 46 101 112 46 112 97 114 108 46 117 110 105 111 110 46 101 117 47 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0'
	}
}
Else{
    [string]$value = (Get-ItemProperty -Path registry::$MORegRouteInfos -ErrorAction SilentlyContinue).$property
    If (!$value){
    	Switch($property){
    	"Destination"{$value="136.173.0.0"} 
    	"Mask"{$value="255.255.0.0"} 
    	"BackedUpDefaultRoute"{$value=""} 
    	}
    }
}  
	If($value.contains(";")){
		return [array]$array=$value.split(";")
	}
	Else{
		return $value
	}  
}

 function Write-MOLogevent{ 
 Param ($Msg, $EntryType, $EventID)
    $date = Get-Date -format dd.MM.yyyy 
    $time = Get-Date -format HH:mm:ss 
    write-eventlog -logname MobileOffice -source WatcherServices -eventID $EventID -entrytype $EntryType -message $Msg -Category ""
 }
	
	$WatcherEnabled = Get_MOReg_Info "ServicesWatcherEnabled"
	if($WatcherEnabled -eq 1)
	{
		$Services2Watch = Get_MOReg_Info "Services2Watch"
		foreach($service in $Services2Watch)
		{
			if ((Get-Service $service).Status -eq 'stopped')
		    {
                $msg = "Service "+$service+" stopped...restarting..."
				Write-MOLogevent $msg "Information" 31
		        Start-Service $service
		    }
		}
	}